// https://leetcode.cn/problems/solve-the-equation/
// Created by ade on 2022/8/10.
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>


using namespace std;

class Solution {
public:
    // 解决思路：所有的-转换为+- 统一移到左侧进行处理
    string solveEquation(string equation) {
        int ePos = equation.find("=");
        string newS = "";
        int start = 0;
        if (equation[0] == '-') {
            newS = "-";
            start = 1;
        }
        for (int i = start; i < ePos; i++) {
            if (equation[i] == '-') {
                newS += "+-";
            } else {
                newS += equation[i];
            }
        }
        // =后面的第一个
        start = ePos + 1;
        if (equation[ePos + 1] == '-') {
            newS += "+";
            start = ePos + 2;
        } else {
            newS += "+-";
        }
        int len = equation.size();
        for (int i = start; i < len; i++) {
            if (equation[i] == '-') {
                newS += "+";
            } else if (equation[i] == '+') {
                newS += "+-";
            } else {
                newS += equation[i];
            }
        }
        vector <string> nums;
        stringstream ss;
        ss << newS;
        string tmp = "";
        int xNum = 0;
        int nNum = 0;
        while (getline(ss, tmp, '+')) {
            if (tmp.find("x") != string::npos) {
                if (tmp == "x") {
                    xNum += 1;
                } else if (tmp == "-x") {
                    xNum += -1;
                } else {
                    xNum += stoi(tmp.substr(0, tmp.size() - 1));
                }
            } else {
                nNum += stoi(tmp);
            }
        }
        if (xNum == 0) {
            if (nNum == 0) {
                return "Infinite solutions";
            } else {
                return "No solution";
            }
        }
        int res = -1 * (nNum / xNum);
        return "x=" + to_string(res);
    }
};

int main() {
    Solution so;
    string a = "-x=-1";
//    string a = "x+5-3+x=6+x-2";
    cout << so.solveEquation(a);
    return 0;
}